package com.example.sunmiv1.adapter;

import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.NewInventoryActivity;
import com.example.sunmiv1.activity.WarehousingActivity;
import com.example.sunmiv1.entity.WarehousingDetailInfo;
import com.example.sunmiv1.utils.MathUtil;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

public class WarehousingAdapter extends BaseAdapter {

    private Context context;
    private List<WarehousingDetailInfo.DataBean> list;
    private ViewHolder viewHolder;
    private ACache aCache;

    public WarehousingAdapter(Context context, List<WarehousingDetailInfo.DataBean> list) {
        this.context = context;
        this.list = list;
        aCache = ACache.get(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private int select = 9999;

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
        convertView = View.inflate(context, R.layout.warehousing_adapter, null);
        viewHolder = new ViewHolder(convertView);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        if (select == position) {
            convertView.setBackgroundResource(R.drawable.red_border);
        } else {
            convertView.setBackgroundResource(R.color.white);
        }
        viewHolder.checkWarehousing.setVisibility(View.GONE);
        WarehousingDetailInfo.DataBean dataBean = list.get(position);
        viewHolder.numWarehousing.setText(context.getResources().getString(R.string.warehousing_num) + (position+1));
        viewHolder.nameWarehousing.setText(dataBean.getGoods_name());
        viewHolder.skuNameWarehousing.setText(dataBean.getSku_name());
        Tools.hideViewInItem(dataBean.getSku_name(),viewHolder.skuNameWarehousing);
        viewHolder.freightNumberWarehousing.setText(dataBean.getNumber());
        viewHolder.barcodeWarehousing.setText(dataBean.getCode());
        viewHolder.assistCountWarehousing.setText("" + dataBean.getStock_num());
        viewHolder.countWarehousing.setText("" + dataBean.getWarehouse_num());
        if ("2".equals(dataBean.getPricing_method())) {//稱重商品
            viewHolder.priceWarehousing.setText("" + dataBean.getWarehouse_price());
        }else{
            viewHolder.priceWarehousing.setText("" + (int)dataBean.getWarehouse_price());
        }
        if (dataBean.getWarehouse_num() > 0 && dataBean.getWarehouse_price() > 0) {
            double p = MathUtil.mul(dataBean.getWarehouse_num(), dataBean.getWarehouse_price());
            viewHolder.totalWarehousing.setText(p + "");
        } else {
            viewHolder.totalWarehousing.setText("0");
        }
        if ("2".equals(aCache.getAsString("showPrice"))) {
            viewHolder.llPriceWarehousing.setVisibility(View.GONE);
            viewHolder.llTotalWarehousing.setVisibility(View.GONE);
        } else {
            viewHolder.llPriceWarehousing.setVisibility(View.VISIBLE);
            viewHolder.llTotalWarehousing.setVisibility(View.VISIBLE);
            if ("1".equals(aCache.getAsString("allocation"))) {
                viewHolder.priceWarehousing.setEnabled(true);
            } else {
                viewHolder.priceWarehousing.setBackground(null);
                viewHolder.priceWarehousing.setEnabled(false);
            }
        }
        if (WarehousingActivity.opera) {
            viewHolder.checkWarehousing.setVisibility(View.VISIBLE);
        } else {
            viewHolder.checkWarehousing.setVisibility(View.GONE);
        }
        viewHolder.checkWarehousing.setOnCheckedChangeListener(null);
        viewHolder.checkWarehousing.setChecked(list.get(position).isChecked());
        viewHolder.checkWarehousing.setOnCheckedChangeListener((buttonView, isChecked) -> {
            list.get(position).setChecked(isChecked);
            notifyDataSetChanged();
        });
        viewHolder.priceWarehousing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if ("2".equals(dataBean.getPricing_method())) {//稱重商品
                    if (charSequence.toString().contains(".")) {
                        if (-1 != charSequence.toString().indexOf(".", charSequence.toString().indexOf(".") + 1)) {
                            viewHolder.priceWarehousing.setText(charSequence.toString().
                                    substring(0, charSequence.toString().length() - 1));
                            viewHolder.priceWarehousing.setSelection(charSequence.length() - 1);
                        } else {
                            if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 2) {
                                charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 3);
                                viewHolder.priceWarehousing.setText(charSequence);
                                viewHolder.priceWarehousing.setSelection(charSequence.length());
                            }
                        }
                    }
                    if (charSequence.toString().trim().substring(0).equals(".")) {
                        charSequence = "0" + charSequence;
                        viewHolder.priceWarehousing.setText(charSequence);
                        viewHolder.priceWarehousing.setSelection(2);
                    }

                    if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                        if (!charSequence.toString().substring(1, 2).equals(".")) {
                            viewHolder.priceWarehousing.setText(charSequence.subSequence(1, 2));
                            viewHolder.priceWarehousing.setSelection(1);
                            return;
                        }
                    }
                } else {
                    if (charSequence.toString().contains(".")) {
                        if (-1 != charSequence.toString().indexOf(".")) {
                            viewHolder.priceWarehousing.setText(charSequence.toString().
                                    substring(0, charSequence.toString().indexOf(".")));
                            viewHolder.priceWarehousing.setSelection(charSequence.length() - 1);
                        }
                    }
                    if (charSequence.toString().trim().substring(0).equals(".")) {
                        charSequence = "0";
                        viewHolder.priceWarehousing.setText(charSequence);
                        viewHolder.priceWarehousing.setSelection(1);
                    }

                    if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                        if (!charSequence.toString().substring(1, 2).equals(".")) {
                            viewHolder.priceWarehousing.setText(charSequence.subSequence(1, 2));
                            viewHolder.priceWarehousing.setSelection(1);
                            return;
                        }
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(viewHolder.priceWarehousing.getText().toString().trim())) {
                    list.get(position).setWarehouse_price(Double.parseDouble(viewHolder.priceWarehousing.getText().toString().trim()));
                    if (list.get(position).getWarehouse_num() > 0 && list.get(position).getWarehouse_price() > 0) {
                        double p = MathUtil.mul(list.get(position).getWarehouse_num(), list.get(position).getWarehouse_price());
                        viewHolder.totalWarehousing.setText(p + "");
                    } else {
                        viewHolder.totalWarehousing.setText("0");
                    }
                } else {
                    viewHolder.totalWarehousing.setText("0");
                }
            }
        });
        viewHolder.countWarehousing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence.toString().contains(".")) {
                    if (-1 != charSequence.toString().indexOf(".", charSequence.toString().indexOf(".") + 1)) {
                        viewHolder.countWarehousing.setText(charSequence.toString().
                                substring(0, charSequence.toString().length() - 1));
                        viewHolder.countWarehousing.setSelection(charSequence.length() - 1);
                    } else {
                        if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 3) {
                            charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 4);
                            viewHolder.countWarehousing.setText(charSequence);
                            viewHolder.countWarehousing.setSelection(charSequence.length());
                        }
                    }
                }
                if (charSequence.toString().trim().substring(0).equals(".")) {
                    charSequence = "0" + charSequence;
                    viewHolder.countWarehousing.setText(charSequence);
                    viewHolder.countWarehousing.setSelection(2);
                }

                if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                    if (!charSequence.toString().substring(1, 2).equals(".")) {
                        viewHolder.countWarehousing.setText(charSequence.subSequence(1, 2));
                        viewHolder.countWarehousing.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(viewHolder.countWarehousing.getText().toString().trim())) {
                    list.get(position).setWarehouse_num(Double.parseDouble(viewHolder.countWarehousing.getText().toString().trim()));
                    if (list.get(position).getWarehouse_num() > 0 && list.get(position).getWarehouse_price() > 0) {
                        double p = MathUtil.mul(list.get(position).getWarehouse_num(), list.get(position).getWarehouse_price());
                        viewHolder.totalWarehousing.setText(p + "");
                    } else {
                        viewHolder.totalWarehousing.setText("0");
                    }
                } else {
                    viewHolder.totalWarehousing.setText("0");
                }
            }
        });
        viewHolder.remarkWarehousing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setRemarks(viewHolder.remarkWarehousing.getText().toString().trim());
            }
        });
        if ("1".equals(list.get(position).getSerial_number_state())) {
            viewHolder.inventorySearchAdapterChooseSerial.setVisibility(View.VISIBLE);
            viewHolder.countWarehousing.setVisibility(View.GONE);
            Log.e(TAG, "onBindViewHolder: 商品的庫存========"+list.get(position).getWarehouse_num() );
//            if (TextUtils.isEmpty(list.get(position).getWarehouse_num())) {
//                holder.inventorySearchAdapterInventoryTv.setText("0");
//            }else{
            viewHolder.inventorySearchAdapterInventoryTv.setText(list.get(position).getWarehouse_num()+"");
//            }
        } else {
            viewHolder.inventorySearchAdapterChooseSerial.setVisibility(View.GONE);
            viewHolder.countWarehousing.setVisibility(View.VISIBLE);
        }
        final int index = position;
        viewHolder.inventorySearchAdapterChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choose.choose(index);
            }
        });
        return convertView;
    }

    private Choose choose;
    public void setChoose(Choose choose) {
        this.choose = choose;
    }

    public interface Choose {
        void choose(int index);
    }

    static class ViewHolder {
        @BindView(R.id.check_warehousing)
        CheckBox checkWarehousing;
        @BindView(R.id.num_warehousing)
        TextView numWarehousing;
        @BindView(R.id.name_warehousing)
        TextView nameWarehousing;
        @BindView(R.id.skuName_warehousing)
        TextView skuNameWarehousing;
        @BindView(R.id.freightNumber_warehousing)
        TextView freightNumberWarehousing;
        @BindView(R.id.barcode_warehousing)
        TextView barcodeWarehousing;
        @BindView(R.id.assistCount_warehousing)
        TextView assistCountWarehousing;
        @BindView(R.id.count_warehousing)
        EditText countWarehousing;
        @BindView(R.id.price_warehousing)
        EditText priceWarehousing;
        @BindView(R.id.ll_price_warehousing)
        LinearLayout llPriceWarehousing;
        @BindView(R.id.total_warehousing)
        TextView totalWarehousing;
        @BindView(R.id.ll_total_warehousing)
        LinearLayout llTotalWarehousing;
        @BindView(R.id.remark_warehousing)
        EditText remarkWarehousing;
        @BindView(R.id.inventorySearch_adapter_chooseSerial)
        LinearLayout inventorySearchAdapterChooseSerial;
        @BindView(R.id.inventorySearch_adapter_choose)
        TextView inventorySearchAdapterChoose;
        @BindView(R.id.inventorySearch_adapter_inventoryTv)
        TextView inventorySearchAdapterInventoryTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
